package com.itheima.consumer.listeners;

import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.naming.Name;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Map;

@Slf4j
@Component
public class MqListener {
//
//    @RabbitListener(queues = "simple.queue")
//    public void listenSimpleQueue(String msg){
//        System.out.println("消费者收到了simple.queue的消息：【" + msg +"】");
//    }

//    @RabbitListener(queues = "work.queue")
//    public void listenWorkQueue1(String msg) throws InterruptedException {
//        System.out.println("消费者1 收到了 q1的消息：【" + msg +"】");
//        Thread.sleep(20);
//    }

//
//    @RabbitListener(queues = "work.queue")
//    public void listenWorkQueue2(String msg) throws InterruptedException {
//        System.err.println("消费者2 收到了 work.queue的消息...... ：【" + msg +"】");
//        Thread.sleep(200);
//    }

//
//    @RabbitListener(queues = "fanout.q1")
//    public void listenFanoutQueue1(String msg) throws InterruptedException {
//        System.out.println("消费者1 收到了 fanout.q1的消息：【" + msg +"】");
//    }
//
//    @RabbitListener(queues = "fanout.q2")
//    public void listenFanoutQueue2(String msg) throws InterruptedException {
//        System.out.println("消费者2 收到了 fanout.q2的消息：【" + msg +"】");
//    }

//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(name = "direct.q1", durable = "true"),
//            exchange = @Exchange(name = "amq.direct", type = ExchangeTypes.DIRECT),
//            key = {"news"}
//    ))
//    public void listenDirectQueue1(String msg) throws InterruptedException {
//        System.out.println("消费者1 收到了 direct.queue1的消息：【" + msg +"】");
//    }

//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(name = "direct.q2", durable = "true"),
//            exchange = @Exchange(name = "amq.direct", type = ExchangeTypes.DIRECT),
//            key = {"q", "q2"}
//    ))
//    public void listenDirectQueue2(String msg) throws InterruptedException {
//        System.out.println("消费者2 收到了 direct.queue2的消息：【" + msg +"】");
//    }
//
//    @RabbitListener(queues = "topic.q1")
//    public void listenTopicQueue1(String msg) throws InterruptedException {
//        System.out.println("消费者1 收到了 topic.q1的消息：【" + msg +"】");
//    }
//
//    @RabbitListener(queues = "topic.q2")
//    public void listenTopicQueue2(String msg) throws InterruptedException {
//        System.out.println("消费者2 收到了 topic.q2的消息：【" + msg +"】");
//    }
//
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(name = "object.queue", durable = "true"),
//            exchange = @Exchange(name = "hmall.direct")))
//    public void listenObject(Map<String, Object> msg) throws InterruptedException {
//        System.out.println("消费者 收到了 object.queue的消息：【" + msg +"】");
//    }

    @RabbitListener(queues = "object.queue")
    public void listenTopicQueue2(Message message) {
        System.out.println("故意错误消息");
        throw new RuntimeException("模拟消息处理失败");
    }

//    @RabbitListener(queues = "object.queue")
//    public void listenTopicQueue2(byte[] msg) throws InterruptedException {
//        System.out.println("Received raw bytes: " + Arrays.toString(msg)); // 打印字节数组
//        String message = new String(msg, StandardCharsets.UTF_8); // 转换为字符串
//        System.out.println("消费者2 收到了的消息：【" + message + "】");
//    }
}
